package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetAgencyDistributorManageListReq;
import com.shop2cn.iapi.sqdistributor.parameter.response.pojo.AgencyDistributorVo;
import com.shop2cn.iapi.sqdistributor.service.GetAgencyDistributorManageListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetAgencyDistributorManageList {
	
	//java接口
	private static GetAgencyDistributorManageListReq getAgencyDistributorManageListReq;
	private static GetAgencyDistributorManageListCall getAgencyDistributorManageListCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分销机构对分销员列表查询");
	}

	@Before
	public void caseUp() {
		getAgencyDistributorManageListReq = new GetAgencyDistributorManageListReq();
		getAgencyDistributorManageListCall = new GetAgencyDistributorManageListCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getAgencyDistributorManageListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();

		List<DistributorRelation> actualResultList = distributorRelationWrapper
				.selectDistributorListByParentIdAndType(
						getAgencyDistributorManageListReq
								.getParentDistributorId(),
						getAgencyDistributorManageListReq.getAgencyType());
		List<AgencyDistributorVo> expectList = getAgencyDistributorManageListCall
				.getAgencyDistributorManageListResponse().getAgencyDistributorManageListVoList();
		
		for (int i = 0; i < expectList.size(); i++) {
			
			AgencyDistributorVo expectResult = expectList.get(i);
			DistributorRelation actualResult = actualResultList.get(i);

			Logger.verifyEquals(expectResult.getDistributorId(),
					actualResult.getDistributor_id(), "验证DistributorId");
			Logger.verifyEquals(expectResult.getDistributorName(),
					actualResult.getDistributor_name(), "验证DistributorName");
			Logger.verifyEquals(expectResult.getDistributorPhone(),
					actualResult.getDistributor_phone(), "验证DistributorPhone");
			Logger.verifyEquals(expectResult.getInviteCount(),
					actualResult.getInvite_count(), "验证InviteCount");
			DataUtil.verifyDateEquals(expectResult.getAddTime(),
					actualResult.getAdd_time(), "验证AddTime");
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAgencyDistributorManageList_001() {
		Logger.start(true, "分销机构对下级分销员列表查询, 多个用户");
		
		try {
			getAgencyDistributorManageListReq.setMerchantId(500865146L);
			getAgencyDistributorManageListReq.setParentDistributorId(1167266153L);
			getAgencyDistributorManageListReq.setAgencyType(1);
			getAgencyDistributorManageListReq.setPageIndex(1);
			getAgencyDistributorManageListReq.setPageSize(10);
			
			getAgencyDistributorManageListCall.setData(getAgencyDistributorManageListReq);
			getAgencyDistributorManageListCall.callService();
			
			Logger.verifyEquals(true, getAgencyDistributorManageListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAgencyDistributorManageList_002() {
		Logger.start(true, "CPS对下级分销员列表查询, 没有符合条件的数据");
		
		try {
			
			getAgencyDistributorManageListReq.setMerchantId(500865146L);
			getAgencyDistributorManageListReq.setParentDistributorId(730171354L);
			getAgencyDistributorManageListReq.setAgencyType(1);
			getAgencyDistributorManageListReq.setPageIndex(1);
			getAgencyDistributorManageListReq.setPageSize(10);
			
			getAgencyDistributorManageListCall.setData(getAgencyDistributorManageListReq);
			getAgencyDistributorManageListCall.callService();
			
			Logger.verifyEquals(true, getAgencyDistributorManageListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetAgencyDistributorManageList_003() {
		Logger.start(true, "子店铺，分销机构对下级分销员列表查询, 多个用户");
		
		try {
			
			long merchantId = ShenqiUserSet.autotest_goodsOwnerId;

			List<Long> distributorList = SqDistributorService
					.buildSqDistributorAgencyRelation(merchantId, 0, 2,
							merchantId, 1);

			Long agencyDistributorId = distributorList.get(0);

			SqDistributorService.setDistributorAgencyConfig(merchantId, 1, 1,
					0, 0);

			// 设置分销机构子分销员配置
			SqDistributorService.setSubDistributorAgencyConfig(merchantId,
					agencyDistributorId, 1);
			
			getAgencyDistributorManageListReq.setMerchantId(ShenqiUserSet.chainStoreGoodsOwnerId);
			getAgencyDistributorManageListReq.setParentDistributorId(agencyDistributorId);
			getAgencyDistributorManageListReq.setAgencyType(1);
			getAgencyDistributorManageListReq.setPageIndex(1);
			getAgencyDistributorManageListReq.setPageSize(10);
			
			getAgencyDistributorManageListCall.setData(getAgencyDistributorManageListReq);
			getAgencyDistributorManageListCall.callService();
			
			Logger.verifyEquals(true, getAgencyDistributorManageListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
